package board;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.text.Position;

public class GamePlayer extends BoardApplet implements MouseListener,
		ComponentListener {
	// Define some parameter:
	// color
	public static final int _BLACK = 0;
	public static final int _WHITE = 1;
	int color = _BLACK;

	public void init() {
		this.configureBoard(9);
		// actionlistener
		this.addMouseListener(this);
		// componentlistener
		this.addComponentListener(this);
		// set do test;
		// this.doTest();
	}

	public void doTest() {
		this.board.putWhiteStone(1, 2);
		this.board.putWhiteStone(1, 3);
		this.board.putWhiteStone(2, 6);
		this.board.putBlackStone(2, 3);
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		int cell = this.board.getCellSize();
		Point base = new Point(this.anchorGrid.x, this.anchorGrid.y);
		// shift base half of cell size
		base.setLocation(base.x - cell / 2, base.y - cell / 2);
		// calculate position.
		int x = (arg0.getX() - base.x) / cell;
		int y = (arg0.getY() - base.y) / cell;
		System.out.println(" ... " + x + " ... " + y);
		this.doPutStone(x, y);
	}

	public void doPutStone(int x, int y) {
		if (x < this.board.getBoardType() && y < this.board.getBoardType()) {
			boolean ret;
			if (this.color == _BLACK) {
				ret = this.board.putBlackStone(x, y);
			} else
				ret = this.board.putWhiteStone(x, y);
			if (ret) {
				this.turnover();
			}

			this.repaint();
		}
	}

	private void turnover() {
		if (this.color == _BLACK)
			this.color = _WHITE;
		else
			this.color = _BLACK;

	}

	@Override
	public void componentHidden(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentMoved(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentResized(ComponentEvent arg0) {
		this.updateAppletSize();
		// System.out.println("updatessize");
	}

	public void updateAppletSize() {
		this.width = this.getSize().width;
		this.height = this.getSize().height;
		int boardsize = ((width < height) ? width : height) * 80 / 100;
		this.board.updateSize(boardsize);
		// set anchor
		anchorBoard.setLocation(5, 5);
		anchorGrid.setLocation(anchorBoard.x + board.getMargin(), anchorBoard.y
				+ board.getMargin());

		System.out.println(this.board.getBoardSize() + "  "
				+ this.board.getCellSize() + "  " + this.board.getMargin()
				+ " " + this.board.getPieceSize());
		this.repaint();
	}

	@Override
	public void componentShown(ComponentEvent arg0) {
		// TODO Auto-generated method stub

	}

}
